package com.biboheart.breport.template.handler;

import com.biboheart.breport.template.BrTemplate;
import com.biboheart.breport.template.BrTemplateHandler;
import com.biboheart.breport.template.BrTemplateStruct;
import com.biboheart.breport.template.struct.BrTemplateStructDefault;
import com.biboheart.brick.utils.CheckUtils;
import com.biboheart.brick.utils.JsonUtils;
import com.biboheart.brick.utils.MapUtils;

import java.util.List;
import java.util.Map;

public class BrTemplateHandlerDefault implements BrTemplateHandler {
    @SuppressWarnings("unchecked")
    @Override
    public BrTemplateStruct handle(BrTemplate template) {
        String templateStr = template.getTemplate();
        if (CheckUtils.isEmpty(templateStr)) {
            return null;
        }
        Map<String, Object> templateMap = JsonUtils.json2objWithType(templateStr, Map.class);
        BrTemplateStructDefault struct = new BrTemplateStructDefault();
        struct.convertColList(MapUtils.getValue(templateMap, "cols", List.class));
        struct.convertParameterList(MapUtils.getValue(templateMap, "params", List.class));
        return struct;
    }
}
